package com.it.travel.aop;

import com.it.travel.pojo.Logs;
import com.it.travel.service.admin.AdminLogsService;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.Date;

@Aspect
@Component
public class LogAop {

    @Resource
    private AdminLogsService adminLogsService;

    //    @Around(value = "@annotation(com.it.travel.common.RequiredPermission)")
    @Around(value = "execution(* com.it.travel.controller.*.*(..))"
    )
    public Object around(ProceedingJoinPoint joinPoint) throws Exception {
        Object proceed = null;
        ServletRequestAttributes requestAttributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        HttpServletRequest request = requestAttributes.getRequest();
        long starttime = System.currentTimeMillis();
        try {
            proceed = joinPoint.proceed();
        } catch (Throwable throwable) {
            throwable.printStackTrace();
        }
        long endtime = System.currentTimeMillis();
        long time = endtime - starttime;
        saveLogs(joinPoint, time, proceed);

        return proceed;
    }

    /**
     * @param joinPoint 连接点，其实就是被增强功能的那个方法
     * @param time      方法执行时间
     * @param proceed   方法返回值
     */
    public void saveLogs(ProceedingJoinPoint joinPoint, Long time, Object proceed) {
        ServletRequestAttributes requestAttributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        HttpServletRequest request = requestAttributes.getRequest();
        StringBuffer requestURL = request.getRequestURL();
        String queryString = request.getQueryString();
//        request.get
//        request.getRequestDispatcher().forward()
        String name = joinPoint.getSignature().getName();
        String declaringName = joinPoint.getSignature().getDeclaringTypeName();
        StringBuilder stringBuilder = new StringBuilder(declaringName);  // 请求方法
        stringBuilder.append("/" + name);
        Logs logs = new Logs();
        String ip = request.getRemoteAddr(); // ip 地址
        String ipRegion = null;
//        try {
//            ipRegion = Ip2Region.getIpRegion(ip);  // ip 的所在地
//        } catch (DbMakerConfigException e) {
//            e.printStackTrace();
//        } catch (IOException ioException) {
//            ioException.printStackTrace();
//        }
        logs.setIp(ip);
//        logs.setAddress(ipRegion);
        logs.setMethod(stringBuilder.toString());
        logs.setParams(queryString);
        logs.setTime(time);
//        logs.setReturnValue(proceed.toString());
//        logs.setOperation(requestURL.toString());
        logs.setUrl(requestURL.toString());
        logs.setCreateTime(System.currentTimeMillis());
        System.out.println("logs = " + logs);
        adminLogsService.insertLogs(logs);
    }
}
